package service

import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.MultiFormatWriter
import com.google.zxing.MatrixToImageWriter

import com.google.zxing.common.BitMatrix
import com.google.zxing.client.j2se.MatrixToImageWriter

// 二维码生成
class QrService {
	final static int defaultWidth = 300
	final static int defaultHeight = 300

	static String output(String content, String filePath) {
		output(content, filePath, defaultWidth, defaultHeight)
	}

	static void output(String content, String filePath, int w, int h) {
		Hashtable<EncodeHintType, String> hints = [:]
		hints.put(EncodeHintType.CHARACTER_SET, 'UTF8')

		BitMatrix bmx = new MultiFormatWriter().encode(content,
					BarcodeFormat.QR_CODE, w, h, hints)

		MatrixToImageWriter.writeToFile(bmx, 'png', new File(filePath))
	}

}